﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Konst3d.Efficiency
{
    // TOFIX: deprecated, remove it
    public class ObservableList<T>: ICollection<T>
    {
        #region Types

        public delegate void ItemCallback(T item);

        #endregion // Types

        #region Events

        public event ItemCallback ItemAdded;
        public event ItemCallback ItemRemoved;

        #endregion // Events

        #region Constructor

        public ObservableList(bool allowDuplicates)
        {
            _duplicatesAllowed = allowDuplicates;
        }

        #endregion // Constructor

        #region ICollection<T> Members

        public void Add(T item)
        {
            if (!_duplicatesAllowed)
            {
                if (Contains(item))
                {
                    throw new InvalidOperationException("Duplicates does not allowed.");
                }
            }

            _list.Add(item);
        }

        public void Clear()
        {
            _list.Clear();
        }

        public bool Contains(T item)
        {
            return _list.Contains(item);
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            _list.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get
            {
                return _list.Count;
            }
        }

        public bool IsReadOnly
        {
            get
            {
                return false;
            }
        }

        public bool Remove(T item)
        {
            return _list.Remove(item);
        }

        public IEnumerator<T> GetEnumerator()
        {
            return _list.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _list.GetEnumerator();
        }

        #endregion // ICollection<T> Members

        #region Fields

        private List<T> _list = new List<T>();
        private bool _duplicatesAllowed = false;

        #endregion // Fields
    }
}
